﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.HumanResources;

namespace ThridGroup.ERP.Web.Controllers.HumanResources
{
    public class TalentRegistrationController : Controller
    {
        private readonly IAddOnboardingReviewServices addOnboardingReviewServices;

        public TalentRegistrationController(IAddOnboardingReviewServices addOnboardingReviewServices)
        {
            this.addOnboardingReviewServices = addOnboardingReviewServices;
        }

        public IActionResult TalentRegistrationView()
        {
            return View();
        }

        /// <summary>
        /// 添加人才登记
        /// </summary>
        /// <param name="humanResourcesDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddHuman([FromBody] TalentRegistrationDTO humanResourcesDTO)
        {
            var result = await addOnboardingReviewServices.AddHuman(humanResourcesDTO);
            return Json(new
            {
                success = result > 0,
                message = result > 0 ? "添加成功" : "添加失败"
            });
        }

        /// <summary>
        /// 人才登记删除
        /// </summary>
        /// <param name="humanId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteHuman(int humanId)
        {
            var result = await addOnboardingReviewServices.DeleteHuman(humanId);
            return Json(new
            {
                success = result > 0,
                message = result > 0 ? "删除成功" : "删除失败"
            });
        }

        /// <summary>
        /// 获取人才登记
        /// </summary>
        /// <param name="humanId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetHumanResources(int humanId)
        {
            var result = await addOnboardingReviewServices.GetHumanResources(humanId);
            return Json(new
            {
                success = true,
                data = result
            });
        }

        /// <summary>
        ///人才登记列表 
        /// </summary>
        /// <param name="showHumanResourcesDTO"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowHumanResources([FromQuery] ShowHumanResourcesDTO showHumanResourcesDTO)
        {
            var result = await addOnboardingReviewServices.ShowHumanResources(showHumanResourcesDTO);
            var HumanResourceslist = result.OrderByDescending(m => m.Id).Skip((showHumanResourcesDTO.PageIndex - 1) * showHumanResourcesDTO.PageCount).Take(showHumanResourcesDTO.PageCount).ToList();
            return Json(new
            {

                code = 0,
                msg = "",
                count = result.Count,
                data = HumanResourceslist
            });
        }


        /// <summary>
        ///  编辑人才登记
        /// </summary>
        /// <param name="humanResourcesDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdateHuman([FromBody] TalentRegistrationDTO humanResourcesDTO)
        {
            var result = await addOnboardingReviewServices.UpdateHuman(humanResourcesDTO);
            return Json(new
            {
                success = result > 0,
                message = result > 0 ? "编辑成功" : "编辑失败"
            });
        }

        /// <summary>
        /// 人才登记的批量删除
        /// </summary>
        /// <param name="humanIds"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DelMany([FromBody]List<int> humanIds) 
        {
        await addOnboardingReviewServices.DelMany(humanIds);
        }
    }
}
